package com.sanhe.a448.activity.work;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.finddreams.languagelib.Audio;
import com.finddreams.languagelib.SoundTouchListener;
import com.menu.app.MyApp;
import com.menu.app.activity.base.MenuBaseActivity;
import com.menu.app.it.M808;
import com.sanhe.a448.R;
import com.sanhe.a448.it.TouchUpListener;

public abstract class BaseWorkActivity extends MenuBaseActivity implements M808 {

    @Override
    protected byte[][] getInterest() {
        return new byte[0][];
    }

    @Override
    protected void updateUI() {

    }

    public void enableAll(final View group, final boolean enable) {
        group.setEnabled(enable);
        if (group instanceof ViewGroup) {
            int j = ((ViewGroup) group).getChildCount();
            for (int i = 0; i < j; i++) {
                View view = ((ViewGroup) group).getChildAt(i);
                if (view instanceof ViewGroup) {
                    enableAll(view, enable);
                } else {
                    view.setEnabled(enable);
                }
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(View view) {
        view.setBackgroundResource(getBackgroundRes());
        super.setContentView(view);
    }

    protected int getBackgroundRes() {
        return R.drawable.ac_bg;
    }

    @Override
    public void exit() {
        finish();
    }

    @Override
    public String getPageIndex() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MyApp.getApp().setIgnore(false);
    }

    class TouchRunnable implements Runnable {
        Runnable run;
        boolean stop = false;

        TouchRunnable(Runnable run) {
            this.run = run;
        }

        public Runnable stop(boolean bo) {
            stop = bo;
            return this;
        }

        @Override
        public void run() {
            if (stop) {
                return;
            }
            if (!SoundTouchListener.silent) {
                Audio.play();
            }
            run.run();
            h.postDelayed(this, 250);
        }
    }

    public void setTouch(View v, Runnable run) {
        TouchRunnable runnable = new TouchRunnable(run);
        v.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        if (!v.isEnabled()) {
                            return true;
                        }
                        if (v instanceof TouchUpListener && ((TouchUpListener) v).onTouchDown()) {
                            return true;
                        }
                        h.post(runnable.stop(false));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        if (!v.isEnabled()) {
                            return true;
                        }
                        if (v instanceof TouchUpListener) {
                            ((TouchUpListener) v).onTouchUp();
                        }
                        runnable.stop(true);
                        h.removeCallbacks(runnable);
                        break;
                }
                return true;
            }
        });
    }
}
